/**
 * @author 谭
 * 书中间间
 */

const db = require('../../db/index');

const returnData = require('../../utils/returnData');

/*
    还书日期
 */
const data = 60;

/*
    获取书的总数
 */
exports.getAllBooks = (req,res,next) => {
    try {
        const sql = 'select count(*) from books where isDel=0';
        db.query(sql,req.body.accounts,(err,result) => {
            if (err) return returnData(res,500,'错误异常');
            req.body.total = result[0]['count(*)']
            next();
        });
    } catch (e) {
        returnData(res,500,'错误异常')
    }
}

/*
    获取记录的总数
 */
exports.getAllBooksRecord = (req,res,next) => {
    try {
        const sql = 'select count(*) from recorduser';
        db.query(sql,(err,result) => {
            if (err) return returnData(res,500,'错误异常');
            req.body.total = result[0]['count(*)'];
            next();
        });
    } catch (e) {
        returnData(res,500,'错误异常');
    }
}

/*
    用户借阅这本书,判断是否这本书已空
 */
exports.judgeBooksIsNull = (req,res,next) => {
    try {
        const sql = 'select * from books where id=?';
        db.query(sql,req.params.booksId,(err,result) => {
            if (err) return returnData(res,500,err);
            if (result.length !== 1)  return returnData(res,500,'错误异常');
            if ((parseInt(result[0].number) - 1) < 0) return returnData(res,500,'对不起，这本书已经被借阅完了');
            const number = parseInt(result[0].number) - 1;
            const updateSql = 'update books set number=? where id=?'
            db.query(updateSql,[number,req.params.booksId],(err,result) => {
                if (err) return returnData(res,500,err);
                if (result.affectedRows !== 1)  return returnData(res,500,'错误异常');
                next()
            })
        })
    } catch (e) {
        returnData(res,500,e);
    }
}

/*
    判断用户是否过期还书
 */
exports.judgeUserBooksRecord = (req,res,next) => {
    try {
        const sql = 'select * from recorduser where id=? and accounts=?';
        db.query(sql,[req.params.recordId,req.user.accounts],(err,result) => {
            if (err) return returnData(res,500,err);
            if (result.length !== 1) return returnData(res,400,'失败');
            let newDate = new Date();
            let startTime = result[0].startTime.substr(0,10);
            newDate = getYMDHMS(newDate);
            let a = count(startTime,newDate)
            const number = parseInt(result[0].number) + 1;
            req.body.number = number;
            req.body.booksId = result[0].booksId;
            if (a > data) { // 大于了还书日期

            } else { // 直接还书
                console.log('ok2')
                next();
            }
        })
    } catch (e) {
        returnData(res,500,'错误异常');
    }
}


function count(date1,date2){
    var date1=new Date(date1)
    var date2=new Date(date2);
    var date=(date2.getTime()-date1.getTime())/(1000*60*60*24);
    return date;
}

function getYMDHMS (timestamp) {
    let time = new Date(timestamp)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    let hours = time.getHours()
    let minute = time.getMinutes()
    let second = time.getSeconds()
    if (month < 10) { month = '0' + month }
    if (date < 10) { date = '0' + date }
    if (hours < 10) { hours = '0' + hours }
    if (minute < 10) { minute = '0' + minute }
    if (second < 10) { second = '0' + second }
    return year + '-' + month + '-' + date
}
